#include "TcpServer.hpp"
#include "Http.hpp"
#include "./LedgerCalculation/LedgerCalculation.h"
#include <iostream>
#include <memory>
#include "Daemon.h"
#include <locale>
#include <codecvt>
#include <iomanip>

void Login(HttpRequest &req, HttpResponse &resp)
{
    LOG(LogLevel::DEBUG) << req.getArgs() << ", 我们成功进入到了处理数据的逻辑";
    std::string text = "hello: " + req.getArgs();

    // 登录认证

    std::string test = req.getArgs();
    auto pos = test.find("=");
    if (pos != std::string::npos)
    {
        test = test.substr(pos + 1);
    }
    LOG(LogLevel::DEBUG) << "test content: " << test;
    
    std::string ans;
	LedgerCalculation testget;
    std::istringstream sin(test);
    std::ostringstream sout;
	testget.input(sin);
	testget.output(sout);
    ans = sout.str();
    resp.setText(ans);
    LOG(LogLevel::DEBUG) << ans;

    resp.setCode(200);
    resp.setHeader("Content-Type","text/plain; charset=utf-8");
    resp.setHeader("Content-Length", std::to_string(ans.size()));
    // resp.setText(text);
}
void Register(HttpRequest &req, HttpResponse &resp)
{
    LOG(LogLevel::DEBUG) << req.getArgs() << ", 我们成功进入到了处理数据的逻辑";
    std::string text = "hello: " + req.getArgs();



    resp.setCode(200);
    resp.setHeader("Content-Type","text/plain");
    resp.setHeader("Content-Length", std::to_string(text.size()));
    resp.setText(text);
}
void VipCheck(HttpRequest &req, HttpResponse &resp)
{
    LOG(LogLevel::DEBUG) << req.getArgs() << ", 我们成功进入到了处理数据的逻辑";
    std::string text = "hello: " + req.getArgs();
    resp.setCode(200);
    resp.setHeader("Content-Type","text/plain");
    resp.setHeader("Content-Length", std::to_string(text.size()));
    resp.setText(text);
}

void test4(HttpRequest &req, HttpResponse &resp)
{
    LOG(LogLevel::DEBUG) << req.getArgs() << ", 我们成功进入到了处理数据的逻辑";
    std::string text = "hello: " + req.getArgs();

    // 登录认证

    resp.setCode(200);
    resp.setHeader("Content-Type","text/plain");
    resp.setHeader("Content-Length", std::to_string(text.size()));
    resp.setText(text);
}

void analyze(const std::string& sou, std::string& dec)
{
    for (int i = 0; i < sou.size(); ++i)
    {
        if (sou[i] == '%')
        {
            std::string aim = sou.substr(i, 3);
            if (aim == "%0D")
            {
                dec += '\r';
            }
            else if (aim == "%0A")
            {
                dec += '\n';
            }
            i += 2;
        }
        else if (sou[i] == '+')
        {
            dec += ' ';
        }
        else
        {
            dec += sou[i];
        }
    }
}

void test5(HttpRequest &req, HttpResponse &resp)
{
    LOG(LogLevel::DEBUG) << req.getArgs() << ", 账本处理";
    LOG(LogLevel::DEBUG) << "req 文本:" << req.getText();
    std::string text;
    if (req.getMethod() == "post" || req.getMethod() == "POST")
    {
        LOG(LogLevel::DEBUG) << "req text content: " << req.getText();
        analyze(req.getText(), text);
        auto pos = text.find("=");
        if (pos != std::string::npos)
        {
            text = text.substr(pos + 1);
        }
        LOG(LogLevel::DEBUG) << "text content: " << text;
    }
    else
    {
        text = req.getArgs();
        auto pos = text.find("=");
        if (pos != std::string::npos)
        {
            text = text.substr(pos + 1);
        }
        LOG(LogLevel::DEBUG) << "text content: " << text;
    }


    std::string ans;
    
	LedgerCalculation testget;


    std::istringstream sin(text);
    std::ostringstream sout;

	testget.input(sin);
	testget.output(sout);
    ans = sout.str();
    resp.setText(ans);
    LOG(LogLevel::DEBUG) << ans;

    resp.setCode(200);
    resp.setHeader("Content-Type","text/plain; charset=utf-8");
    resp.setHeader("Content-Length", std::to_string(ans.size()));
}

int main(int argc, char* argv[])
{
    std::locale::global(std::locale("en_US.UTF-8"));
    std::wcout.imbue(std::locale());
    
    if (argc != 2)
    {
        std::cerr << "格式输入方式: ./tcpServer port" << std::endl;
        return 1;
    }

    uint16_t port = std::stoi(argv[1]);

    // Daemon(0, 0);
    
    ENABLE_FILE_LOG_STRATEGY();

    std::unique_ptr<HttpProtocol> protocol = std::make_unique<HttpProtocol>(port);

    protocol->registerService("/login", Login);
    // protocol->registerService("/register", Register);
    // protocol->registerService("/vip_check", VipCheck);
    protocol->registerService("/LedgerCalculation", test5);

    protocol->start();
    
    return 0;
}
